/**
 * Created by zoulu on 15/10/14.
 */

var Manage = {
	init: function() {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = "";
		if (url.indexOf("?") != -1) {
			theRequest = url.split("=")[1];
		}
		$("#update_venue").find("input[name='schoolid']").attr("value",theRequest);
		Manage.get_data(theRequest);
		
	
		$("a[name='add_venue']").click(Manage.venue_show);
		$("#data_table tbody").find("a[name='update_venue']").click(Manage.venue_show);
		$("#data_table tbody").find("a[name='delete']").click(Manage.delete);
		
		$("#do_update_venue").click(function() {
			var id = $("#update_venue").find("input[name='id']").val();
			var name = $("#update_venue").find("input[name='name']").val();
			var number = $("#update_venue").find("input[name='number']").val();
			var location = $("#update_venue").find("input[name='location']").val();
			var capacity = $("#update_venue").find("input[name='capacity']").val();
			var area = $("#update_venue").find("input[name='area']").val();
			var schoolid = $("#update_venue").find("input[name='schoolid']").val();
			
			if (name == null || name == "") {
				alert("名称不能为空");
				return false;
			}
			if (number == null || number == "") {
				alert("教室编码不能为空");
				return false;
			}
			if (area == null || area == "") {
				alert("面积不能为空");
				return false;
			}
			
			if (id == "" || id == 0) {
				//新增
				ajax("/rooms/create", {
					"number": number,
					"name": name,
					"area": area,
					"capacity": capacity,
					"location": location,
					"school_id": schoolid,
					
					"wiki": ""
				
				}, success);
			} else {
				//编辑
				ajax("/rooms/update", {
					"number": number,
					"name": name,
					"area": area,
					"capacity": capacity,
					"location": location,
					
					
					"wiki": "",
					"room_id": id
				}, success);
			}
		});
		$("#do_add_class").click(function() {
			var venue_id = $("#add_class").find("input[name='venue_id']").val();
			var class_id = $("#add_class").find("select[name='class_id']").val();
			if (class_id == null || class_id == "") {
				alert("必须选择一个科目");
			}
			ajax("/categories/classes/add", {
				"venue_id": venue_id,
				"class_ids": class_id
			}, success);
		});
	},
	delete: function() {
		var  id= $(this).attr("id");
		var school_id = $("#add_photo").find("input[name='school_id']").val();
		if (confirm("确定要删除教室?")) {

			ajax("/rooms/remove", {
				"room_id": id
				
			}, success);
		}
	},
	get_data: function(schoolid) {
		
		ajax("/schools/rooms/list", {
			"school_id": schoolid,
		}, callback_init_table);
	},
	venue_show: function() {
		var id = $(this).attr("id");
		var name = $(this).attr("venuename");
		var capacity = $(this).attr("capacity");
		var area = $(this).attr("area");
		var location = $(this).attr("location");
		var number = $(this).attr("number");
	
		$("#update_venue").find("input[name='id']").val(id);
		$("#update_venue").find("input[name='name']").val(name);
		$("#update_venue").find("input[name='capacity']").val(capacity);
		$("#update_venue").find("input[name='area']").val(area);
		$("#update_venue").find("input[name='location']").val(location);
		$("#update_venue").find("input[name='number']").val(number);
	
		$("#update_venue").modal('show');
	},
	
	class_show: function() {
		var venue_id = $(this).attr("venue_id");
		$("#add_class").find("input[name='venue_id']").val(venue_id);
		ajax("/classes/list", {}, callback_init_class);
		$("#add_class").show();
	},
	init_brands: function(result) {
		if (result != null && result.data != null && result.data.brands.length > 0) {
			var brands = result.data.brands;
			var trs = "";
			for (var i = 0; i < brands.length; i++) {
				var varItem = "<option value ='" + brands[i].id + "'>" + brands[i].name + "</option>";
				$("#brand_id").append(varItem);
			}
		}
	},

	init_table: function(result) {
		if (result != null && result.data != null && result.data.rooms.length > 0) {
			var tbody = $("#data_table tbody");
			tbody.empty();
			var rooms = result.data.rooms;
		
			var trs = "";
			for (var i = 0; i < rooms.length; i++) {
				var venue = rooms[i];

				trs += ("<tr>");
				var number = ("<td >" +venue.number + "</td>");
				var name = ("<td >" + venue.name + "</td>");
				var location = ("<td >" + venue.location + "</td>");
				var area = ("<td >" + venue.area + "</td>");
				var capacity = ("<td >" + venue.capacity + "</td>");
			


				var venueOperate = ("<td>" +
					"<a number='" + venue.number + "' venuename='" + venue.name + "' location='" + venue.location + "' area='" + venue.area + "' capacity='" + venue.capacity +
					"' id='" + venue.id +
					"' name='update_venue' href='javascript:void(0)'>更新场馆</a>" +
					"&nbsp;&nbsp;<a ' id='" + venue.id +"' name='delete' href='javascript:void(0)'>删除</a>" +
					"&nbsp;&nbsp;<a name='select_photos' href='./room_photo.html?school_id=" + venue.id + "'>查看教室图片</a>" +
					"</td>");
				trs += (number + name + location + area + capacity + venueOperate);
				trs += ("</tr>");
			}
			tbody.append(trs);
		}
	},
	init_class: function(result) {
		if (result != null && result.data != null && result.data.classes.length > 0) {
			var class_id = $("#add_class").find("[name=class_id]");
			class_id.empty();
			var classes = result.data.classes;
			var options = "";
			for (var i = 0; i < classes.length; i++) {
				var classOne = classes[i];
				options += ("<option value = " + classOne.id + ">" + classOne.name + "</option>");
			}
			class_id.append(options);
		}
	}
};

function callback_init_brands(result) {
	Manage.init_brands(result);
}

function callback_init_table(result) {
	Manage.init_table(result);
}

function callback_init_class(result) {
	Manage.init_class(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function() {
	$("#datetimepicker3").on("click", function(e) {
		e.stopPropagation();
		$(this).lqdatetimepicker({
			css: 'datetime-day',
			dateType: 'D',
			selectback: function() {

			}
		});

	});
	$("#datetimepicker4").on("click", function(e) {
		e.stopPropagation();
		$(this).lqdatetimepicker({
			css: 'datetime-day',
			dateType: 'D',
			selectback: function() {

			}
		});

	});
});

$(function() {
	Manage.init();
})